package algorithm.slidingWindow;

// 将 x 减到 0 的最小操作数
public class LC1658 {
    public int minOperations(int[] nums, int x) {
        int l = 0, r = 0, n = nums.length, target = 0;
        for (int num : nums) target += num;
        target -= x;
        if (target < 0) return -1;

        int sum = 0, len = -1;
        while (r < n) {
            sum += nums[r];
            while (sum > target)
                sum -= nums[l++];
            if (sum == target)
                len = Math.max(len, r - l + 1);
            r++;
        }
        return len == -1 ? -1 : n - len;
    }

    public static void main(String[] args) {
        LC1658 lc1658 = new LC1658();
        int[] nums = {8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309};
        System.out.println(lc1658.minOperations(nums, 134365));
    }
}
